<?php
namespace App\Services;
use App\Models\Balance;
use App\Models\Config;
use App\Models\Merchant;
use App\Models\User;

class BalanceService {

    public static function store($benefit_user, $user_id, $merchant_id, $total_amount, $type, $order_id, $order_sn, $title, $is_transaction = true, $withdraw_id = 0, $extra=null) {
        try {
            $is_transaction && \DB::beginTransaction();
            $merchant = $parent_user = null;
            if ($benefit_user == User::BENEFIT_DAREN) {
                $search_key = 'merchant_id';
                $merchant = Merchant::lockForUpdate()->find($merchant_id);
                $create = true;
            }
            else {
                $create = false;
                $search_key = 'parent_user_id';
                $parent_user = User::lockForUpdate()->find($merchant_id);
                if ($parent_user->is_daren || $parent_user->is_temp_daren) {
                    $create = true;
                }
            }
            if ($create) {
                if ($merchant || $parent_user) {
                    $platform_commission = 0;
                    if ($type == Balance::TYPE_VIP)
                        $amount = $total_amount * Config::getVipCommission();
                    else if($type == Balance::TYPE_COOKBOOK_REC){
                        $amount = $total_amount * Config::getRecCookbookCommission();
                        $platform_commission = $total_amount * Config::getRecCookbookCommission();
                    } else if($type == Balance::TYPE_COOKBOOK) {
                        $amount = $total_amount * Config::getMerchantCommission($order_id);
                    } else if($type == Balance::TYPE_MALL) {
                        $amount = $extra;
                    } else {
                        $amount = $total_amount;
                    }
                    $balanceModel = Balance::where($search_key, $merchant_id)->orderBy('id', 'desc')->first();
                    if ($balanceModel) {
                        $balanceModel = Balance::where('id', $balanceModel->id)->first();
                    }
                    $balance = ($balanceModel->balance ?? 0) + $amount ;
                    $data = [
                        'benefit_user' => $benefit_user,
                        'title' => $title,
                        'user_id' => $user_id,
                        'paid_total_amount' => $total_amount,
                        'platform_commission' => $platform_commission,
                        'order_id' => $order_id,
                        'order_sn' => $order_sn,
                        'withdraw_id' => $withdraw_id,
                        'type' => $type,
                        'amount' => $amount,
                        'balance' => $balance,
                        'extra' => $extra
                    ];
                    if ($benefit_user == User::BENEFIT_DAREN) {
                        $data['merchant_id'] = $merchant_id;
                        Balance::create($data);
                        $merchant->balance = $balance;
                        $merchant->save();
                    } else {
                        $data['parent_user_id'] = $merchant_id;
                        Balance::create($data);
                        $parent_user->balance = $balance;
                        $parent_user->save();
                    }
                }
            }
            $is_transaction && \DB::commit();
        } catch (\Exception $e) {
            $is_transaction && \DB::rollBack();
            throw new \Exception($e->getMessage());
        }

    }

}
